package sbu.cs.ui.widget;

import sbu.cs.IUserIdChangeObserver;
import sbu.cs.R;
import sbu.cs.asdb.ASDBUtils;
import android.content.Context;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UserIdView extends LinearLayout {

	private EditText editTextSigninUserName;
	private TextView labelSigninUserName;
	
	private EditText editTextSigninPassword;
	private TextView labelSigninPassword;

	private EditText editTextRegUserName;
	private TextView labelRegUserName;

	private EditText editTextRegPassword;
	private TextView labelRegPassword;

	private EditText editTextConfirmPassword;
	private TextView labelConfirmPassword;
	
	private TextView labelRegisterMessage;
	private TextView labelSigninMessage;
	/*
	private EditText editTextRegUserName;
	private TextView labelRegUserName;

	private EditText editTextReg;
	private TextView labelRegUserName;

	private EditText editTextSigninUserName;
	private EditText editTextSigninUserPasswordReq;
	private EditText editTextSigninUserPasswordConfirm;

	private TextView labelSigninUserName;
	private TextView labelSigninUserPasswordReg;
	private TextView labelSigninUserPasswordConfirm;
	*/
	private Button regButton;
	private Button signinButton;
	// private Button mCancelButton;

	private static final int ID_ED_SIGNIN_USER_NAME = 100;
	private static final int ID_TV_SIGNIN_USER_NAME = 101;
	
	private static final int ID_ED_SIGNIN_PASSWORD = 102;
	private static final int ID_TV_SIGNIN_PASSWORD = 103;
	
	private static final int ID_BUTTON_SIGNIN = 104;
	
	private static final int ID_ED_REG_USER_NAME = 105;
	private static final int ID_TV_REG_USER_NAME = 106;
	
	private static final int ID_ED_REG_PASSWORD = 107;
	private static final int ID_TV_REG_PASSWORD = 108;
	
	private static final int ID_ED_CONFRIM_PASSWORD = 109;
	private static final int ID_TV_CONFIRM_PASSWORD = 110;
	
	private static final int ID_BUTTON_REG = 111;
	/*
	private static final int ID_EDITTEXT_REG_USER_NAME = 100;
	private static final int ID_REGBUTTON = 101;
	private static final int ID_EDITTEXT_SIGNIN_USER_NAME = 102;
	private static final int ID_EDITTEXT_SIGNIN_PSWD_REQ = 103;
	private static final int ID_EDITTEXT_SIGNIN_PSWD_CONFIRM = 104;
	private static final int ID_SIGNINBUTTON = 105;
	private static final int ID_LABEL_REG_USER_NAME = 106;
	private static final int ID_LABEL_SIGNIN_USER_NAME = 107;
	private static final int ID_LABEL_SIGNIN_PSWD_REG = 108;
	private static final int ID_LABEL_SIGNIN_PSWD_CONFIRME = 109;
	*/
	
	// private static final int ID_CANCELBUTTON = 102;

	private IUserIdChangeObserver userIdChangeObserver;
	private OnClickListener clickListener = new OnClickListener() {

		public void onClick(View v) {
			switch(v.getId()){
			case ID_BUTTON_REG:
				String userName = editTextRegUserName.getText().toString();
				String password = editTextRegPassword.getText().toString();
				String confirmPassword = editTextConfirmPassword.getText().toString();
				
				if(userName.length() == 0 || password.length() == 0 || confirmPassword.length() == 0){
					 Toast t = Toast.makeText(getContext(),
							"Invalid fields. Please retry ",
							Toast.LENGTH_SHORT);
					t.show();
					editTextConfirmPassword.setText("");
					editTextRegPassword.setText("");
					editTextRegUserName.setText("");
					editTextSigninPassword.setText("");
					editTextSigninUserName.setText("");
				} else if (!password.equals(confirmPassword)){
					Toast t = Toast.makeText(getContext(),
							"Passwords do not match. Please retry.",
							Toast.LENGTH_SHORT);
					t.show();
					editTextConfirmPassword.setText("");
					editTextRegPassword.setText("");
					editTextRegUserName.setText("");
					editTextSigninPassword.setText("");
					editTextSigninUserName.setText("");
				} else if (ASDBUtils.createASDBUtils().userNameExists(userName)) {
					Toast t = Toast.makeText(getContext(),
							"User id already in use, choose a different one.",
							Toast.LENGTH_SHORT);
					t.show();
					editTextConfirmPassword.setText("");
					editTextRegPassword.setText("");
					editTextRegUserName.setText("");
					editTextSigninPassword.setText("");
					editTextSigninUserName.setText("");
				} else {
					userIdChangeObserver.onUserIdChanged(userName, password, true);
					setVisibility(GONE);
				}
				break;
			case ID_BUTTON_SIGNIN:
				String signinUserName = editTextSigninUserName.getText().toString();
				String signinPassword = editTextSigninPassword.getText().toString();
				
				if(signinUserName == "" || signinPassword == ""){
					Toast t = Toast.makeText(getContext(),
							"Invalid fields. Please retry.",
							Toast.LENGTH_SHORT);
					t.show();
					editTextConfirmPassword.setText("");
					editTextRegPassword.setText("");
					editTextRegUserName.setText("");
					editTextSigninPassword.setText("");
					editTextSigninUserName.setText("");
				} else if(ASDBUtils.createASDBUtils().verifyLogin(signinUserName, signinPassword)){
					userIdChangeObserver.onUserIdChanged(signinUserName, signinPassword, false);
					setVisibility(GONE);
				} else{
					Toast t = Toast.makeText(getContext(),
							"User id and password do not match. Please retry.",
							Toast.LENGTH_SHORT);
					t.show();
					editTextConfirmPassword.setText("");
					editTextRegPassword.setText("");
					editTextRegUserName.setText("");
					editTextSigninPassword.setText("");
					editTextSigninUserName.setText("");
				}
				break;
			}
			/*
			String userName = editTextRegUserName.getText().toString();
			if (ASDBUtils.createASDBUtils().userNameExists(userName)) {
				Toast t = Toast.makeText(getContext(),
						"User id already in use, choose a different one ",
						Toast.LENGTH_SHORT);
				t.show();
				editTextRegUserName.setText("");
			} else {
				userIdChangeObserver.onUserIdChanged(userName);
				setVisibility(GONE);
			}
			*/
		}
	};

	public UserIdView(Context context) {
		super(context);
		doInit();
	}

	public void setUserIdChangeObserver(IUserIdChangeObserver obs) {
		userIdChangeObserver = obs;
	}

	private void doInit() {
		regButton = new Button(getContext());
		
		editTextSigninUserName = new EditText(getContext());
		labelSigninUserName = new TextView(getContext());
	
		editTextSigninUserName.setId(ID_ED_SIGNIN_USER_NAME);
		labelSigninUserName.setId(ID_TV_SIGNIN_USER_NAME);
	
		editTextSigninPassword= new EditText(getContext());
		labelSigninPassword = new TextView(getContext());
	
		editTextSigninPassword.setId(ID_ED_SIGNIN_PASSWORD);
		labelSigninPassword.setId(ID_TV_SIGNIN_PASSWORD);
	
		signinButton = new Button(getContext());
		signinButton.setId(ID_BUTTON_SIGNIN);
		signinButton.setOnClickListener(clickListener);
		signinButton.setText("Sign in");
			
		regButton = new Button(getContext());
		regButton.setId(ID_BUTTON_REG);
		regButton.setOnClickListener(clickListener);
		regButton.setText("Register");
		
		editTextRegUserName = new EditText(getContext());
		labelRegUserName = new TextView(getContext());
	
		editTextRegUserName.setId(ID_ED_REG_USER_NAME);
		labelRegUserName.setId(ID_TV_REG_USER_NAME);
	
		editTextRegPassword= new EditText(getContext());
		labelRegPassword = new TextView(getContext());
	
		editTextRegPassword.setId(ID_ED_REG_PASSWORD);
		labelRegPassword.setId(ID_TV_REG_PASSWORD);
	
		editTextConfirmPassword= new EditText(getContext());
		labelConfirmPassword = new TextView(getContext());
	
		editTextConfirmPassword.setId(ID_ED_CONFRIM_PASSWORD);
		labelConfirmPassword.setId(ID_TV_CONFIRM_PASSWORD);
		
		labelRegisterMessage = new TextView(getContext());
		labelSigninMessage = new TextView(getContext());
		
		setOrientation(VERTICAL);
		/*
		editTextSigninUserName = new EditText(getContext());
		
		editTextRegUserName = new EditText(getContext());
		editTextSigninUserPasswordReq = new EditText(getContext());
		editTextSigninUserPasswordConfirm = new EditText(getContext());
		signinBUtton = new Button(getContext());

		labelRegUserName = new TextView(getContext());
		labelSigninUserName = new TextView(getContext());
		labelSigninUserPasswordConfirm = new TextView(getContext());
		labelSigninUserPasswordReg  = new TextView(getContext());
		
		// mCancelButton = new Button(getContext());

		editTextRegUserName.setId(ID_EDITTEXT_REG_USER_NAME);
		regButton.setId(ID_REGBUTTON);
		editTextSigninUserName.setId(ID_EDITTEXT_SIGNIN_USER_NAME);
		editTextSigninUserPasswordConfirm
				.setId(ID_EDITTEXT_SIGNIN_PSWD_CONFIRM);
		editTextSigninUserPasswordReq.setId(ID_EDITTEXT_SIGNIN_PSWD_REQ);
		signinBUtton.setId(ID_SIGNINBUTTON);

		labelRegUserName.setId(ID_LABEL_REG_USER_NAME);
		labelSigninUserName.setId(ID_LABEL_SIGNIN_USER_NAME);
		labelSigninUserPasswordConfirm.setId(ID_LABEL_SIGNIN_PSWD_CONFIRME);
		labelSigninUserPasswordReg.setId(ID_LABEL_SIGNIN_PSWD_REG);

		labelRegUserName.setText("User Id");
		labelRegUserName.setTextSize(20);
		regButton.setOnClickListener(clickListener);
		signinBUtton.setOnClickListener(clickListener);
	*/
		RelativeLayout rl = new RelativeLayout(getContext());
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		labelRegisterMessage.setText("New user? Please register");
		labelRegisterMessage.setTextSize(25);
		rl.addView(labelRegisterMessage);
		addView(rl);
				
		labelRegUserName.setText("User Id");
		labelRegUserName.setTextSize(20);
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rl.addView(labelRegUserName,lp);
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.RIGHT_OF, ID_TV_REG_USER_NAME);
		lp.leftMargin=2;
		rl.addView(editTextRegUserName, lp);
		
		addView(rl);
		
		labelRegPassword.setText("Password");
		labelRegPassword.setTextSize(20);
		editTextRegPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rl.addView(labelRegPassword,lp);
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.RIGHT_OF, ID_TV_REG_PASSWORD);
		lp.leftMargin=2;
		rl.addView(editTextRegPassword, lp);
		
		addView(rl);
		
		labelConfirmPassword.setText("Confirm Password");
		labelConfirmPassword.setTextSize(20);
		editTextConfirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(
				90, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rl.addView(labelConfirmPassword,lp);
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.RIGHT_OF, ID_TV_CONFIRM_PASSWORD);
		lp.leftMargin=2;
		rl.addView(editTextConfirmPassword, lp);
		
		addView(rl);
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		rl.addView(regButton, lp);
		
		addView(rl);
		
		rl = new RelativeLayout(getContext());
		labelSigninMessage.setText("Already registered? Please sign-in");
		labelSigninMessage.setTextSize(25);
		rl.addView(labelSigninMessage);
		addView(rl);
		
		labelSigninUserName.setText("User Id");
		labelSigninUserName.setTextSize(20);
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rl.addView(labelSigninUserName,lp);
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.RIGHT_OF, ID_TV_SIGNIN_USER_NAME);
		lp.leftMargin=2;
		rl.addView(editTextSigninUserName, lp);
		
		addView(rl);
		
		labelSigninPassword.setText("Password");
		labelSigninPassword.setTextSize(20);
		editTextSigninPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_VERTICAL);
		rl.addView(labelSigninPassword,lp);
		lp = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.RIGHT_OF, ID_TV_SIGNIN_PASSWORD);
		lp.leftMargin=2;
		rl.addView(editTextSigninPassword, lp);
		
		addView(rl);
		
		rl = new RelativeLayout(getContext());
		lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		rl.addView(signinButton, lp);
		
		addView(rl);
		//rl.addView(la);
		// mCancelButton.setId(ID_CANCELBUTTON);
		/*
		 * RelativeLayout.LayoutParams etParam = new
		 * RelativeLayout.LayoutParams(
		 * RelativeLayout.LayoutParams.MATCH_PARENT,
		 * RelativeLayout.LayoutParams.WRAP_CONTENT);
		 * //etParam.addRule(CENTER_IN_PARENT);
		 * addView(editTextRegUserName,etParam);
		 * 
		 * etParam = new RelativeLayout.LayoutParams(
		 * RelativeLayout.LayoutParams.MATCH_PARENT,
		 * RelativeLayout.LayoutParams.WRAP_CONTENT); etParam.addRule(BELOW,
		 * editTextRegUserName.getId());
		 * addView(editTextSigninUserPasswordConfirm, etParam);
		 * 
		 * etParam = new RelativeLayout.LayoutParams(
		 * RelativeLayout.LayoutParams.MATCH_PARENT,
		 * RelativeLayout.LayoutParams.WRAP_CONTENT); etParam.addRule(BELOW,
		 * editTextSigninUserPasswordConfirm.getId());
		 * addView(editTextSigninUserPasswordReq, etParam);
		 * 
		 * regButton.setText(R.string.useridview_regbuttontext);
		 * RelativeLayout.LayoutParams btParam = new
		 * RelativeLayout.LayoutParams(
		 * RelativeLayout.LayoutParams.WRAP_CONTENT,
		 * RelativeLayout.LayoutParams.WRAP_CONTENT); btParam.addRule(BELOW,
		 * editTextSigninUserPasswordReq.getId()); //btParam.addRule(RIGHT_OF,
		 * regButton.getId()); btParam.addRule(ALIGN_PARENT_LEFT);
		 * btParam.addRule(ALIGN_PARENT_RIGHT); //btParam.leftMargin=85;
		 * addView(regButton, btParam);
		 */
	}

}
